<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${packageName}.${moduleName}.dao.${ClassName}Dao">
	<resultMap id="${className}Map" type="${ClassName}">
		<id column="ID" property="id" jdbcType="VARCHAR" />
		<#list formFieldList as po>
			<#if po.fieldNameLC != 'id'>
		<result column="${po.fieldName}" property="${po.fieldNameLC}" jdbcType="${po.fieldJdbcType}" />
			</#if>
		</#list>
	</resultMap>
	
	<!-- ${functionName}sql字段 -->
	<sql id="${className}_column_sql">
	<#list formFieldList as po>
		${className}.${po.fieldName}<#if po_has_next>,</#if>
	</#list>
	</sql>
	
	<!-- ${functionName} FROM sql-->
	<sql id="${className}_from_sql">
		FROM ${tableName} ${className}
		WHERE 
		<#if formHead.hisSign == 'true'>
			DELETE_TAG != '1'
		<#else>
			1 = 1
		</#if>
		<#if formHead.existSearchForm>
			<#list formFieldList as po>
			<#if po.querySign == 'true'>
			<#if po.fieldJavaType == 'Date'>
			<if test="${po.fieldNameLC}Start != null and ${po.fieldNameLC}Start != ''" >
	      	<![CDATA[
	       		AND ${className}.${po.fieldName} >= TO_DATE(${r"#{"}${po.fieldNameLC}Start${r"}"},'yyyy-MM-dd')
	       			]]>
	      	</if>
	      	<if test="${po.fieldNameLC}End != null and ${po.fieldNameLC}End != ''" >
	      	<![CDATA[
	       		AND ${className}.${po.fieldName} < (TO_DATE(${r"#{"}${po.fieldNameLC}End${r"}"},'yyyy-MM-dd') + 1)
	       			]]>
	      	</if>
			<#else>
			<if test="${po.fieldNameLC} != null and ${po.fieldNameLC} != ''" >
		  		AND ${className}.${po.fieldName} LIKE '%'||${r"#{"}${po.fieldNameLC}${r"}"}||'%'
		  	</if>
			</#if>
		  	</#if>
		  	</#list>
		</#if>
	</sql>

	<!-- 根据ID获取${functionName} -->
	<select id="getById" resultMap="${className}Map" parameterType="java.lang.String">
		SELECT
			<include refid="${className}_column_sql" />
		FROM ${tableName} ${className}
		WHERE ${className}.ID = ${r"#{id}"}
	</select>

	<!-- 保存${functionName}-->
	<insert id="save" parameterType="${ClassName}">
		INSERT INTO ${tableName} (
		<#if formHead.hisSign == 'true'>
			<#list formHead.tbFormFieldListNoHis as po>
			${po.fieldName},
			</#list>
			CREATE_ID,
			CREATE_TIME,
			DELETE_TAG
		<#else>
			<#list formFieldList as po>
			${po.fieldName}<#if po_has_next>,</#if>
			</#list>
		</#if>
		) VALUES (
		<#if formHead.hisSign == 'true'>
			<#list formHead.tbFormFieldListNoHis as po>
			${r"#{"}${po.fieldNameLC}${r"}"},
			</#list>
			${r"#{createId}"},
			SYSDATE,
			'0'
		<#else>
			<#list formFieldList as po>
			${r"#{"}${po.fieldNameLC}${r"}"}<#if po_has_next>,</#if>
			</#list>
		</#if>
		)
	</insert>

	<!-- 更新${functionName}-->
	<update id="update" parameterType="${ClassName}">
		UPDATE
			${tableName}
		<set>
		<#if formHead.hisSign == 'true'>
		<#list formHead.tbFormFieldListNoHis as po>
			<#if po.fieldNameLC != 'id'>
			<if test="${po.fieldNameLC} != null">
			${po.fieldName} = ${r"#{"}${po.fieldNameLC}${r"}"},
			</if>
			</#if>
		</#list>
			UPDATE_ID = ${r"#{updateId}"},
			UPDATE_TIME = SYSDATE
		<#else>
		<#list formFieldList as po>
			<#if po.fieldNameLC != 'id'>
			<if test="${po.fieldNameLC} != null">
			${po.fieldName} = ${r"#{"}${po.fieldNameLC}${r"}"}<#if po_has_next>,</#if>
			</if>
			</#if>
		</#list>
		</#if>
		</set>
		WHERE
			ID = ${r"#{id}"}
	</update>
	
	<#if formHead.hisSign == 'true'>
	<!-- 根据idList,更新删除标志为1 -->
	<update id="deleteTagByIdList">
		UPDATE
			${tableName}
		<set>
			DELETE_TAG = '1',
			DELETE_ID = ${r"#{userId}"},
			DELETE_TIME = SYSDATE
		</set>
		WHERE 
			ID IN
		<foreach item="id" collection="idList" open="(" separator=","
			close=")">
			${r"#{id}"}
		</foreach>
	</update>
	
	<#else>
	<!-- 根据idList删除${functionName} -->
	<delete id="deleteByIdList">
		DELETE FROM
			${tableName}
		WHERE 
			ID IN
		<foreach item="id" collection="list" open="(" separator=","
			close=")">
			${r"#{id}"}
		</foreach>
	</delete>
	</#if>

	<!-- 获取分页 -->
	<select id="findPage" resultMap="${className}Map" parameterType="${ClassName}RTO">
		SELECT
			<include refid="${className}_column_sql" />
			<include refid="${className}_from_sql" />
	</select>
	
</mapper>